const path = require('path')
const CompressionPlugin = require('compression-webpack-plugin')
const resolve = (dir) => {
	return path.join(__dirname, dir)
}

module.exports = {
	publicPath: process.env.VUE_APP_APPURL,
	lintOnSave: true,
	css: {
		loaderOptions: {
			less: {
				lessOptions: {
					javascriptEnabled: true,
					math: 'always'
				}
			}
		}
	},
	configureWebpack: (config) => {
		if (process.env.NODE_ENV === 'development') {
			return {
				devtool: 'source-map'
			}
		}
		const plugins = []
		if (process.env.NODE_ENV === 'production') {
			// 生产
			plugins.push(
				// new SpeedMeasurePlugin(),
				// new BundleAnalyzerPlugin(),
				new CompressionPlugin({
					test: /\.(js|css|json|html)(\?.*)?$/i,
					// filename: '[path][].gz[query]', // 压缩后的文件名
					algorithm: 'gzip',
					threshold: 10240, // 仅处理大于此大小的资产（以字节为单位）
					minRatio: 0.8, // 仅压缩比该比率更好的资产（minRatio = Compressed Size / Original Size）
					deleteOriginalAssets: false // 是否删除原始文件
				})
			)
		}
		return {
			plugins
		}
	},
	chainWebpack: (config) => {
		if (process.env.NODE_ENV === 'production') {
			// 生产
			// 删除console、debugger、注释
			const terser = config.optimization.minimizer('terser')
			terser.tap((args) => {
				const { terserOptions } = args[0]
				Object.assign(terserOptions, {
					compress: {
						...terserOptions.compress,
						drop_console: true,
						drop_debugger: true
					},
					format: {
						comments: /@license/i
					}
				})
				return args
			})
		}
		// 别名配置
		config.resolve.alias
			.set('@', resolve('src')) // key,value自行定义，比如.set('@@', resolve('src/components'))
			.set('_c', resolve('src/components'))
		// eslint-disable-next-line prettier/prettier
		// if (process.env.Node_ENV === 'production') {
		// 	config.plugin('BundleAnalyzerPlugin').use(BundleAnalyzerPlugin)
		// }
	},
	// 设为false打包时不生成.map文件
	productionSourceMap: !process.env.VUE_APP_DEBUG
}
